from pyecharts import options as opts
from pyecharts.charts import Bar,Pie,WordCloud, Grid, Line
# 以下是自定义
from Data_analysis import JD_ciyun, xiaoliang, zonghe, price_best, fx_last, pinglun


# 1词云显示-----实现
# 2综合：默认列表前三十的，--实现---柱形图列出前十；
# 销量(按照评论数随机加100w级别排序)，名称，简介，价格，销量（评论数+100W上下）柱形图正常
# 评论数列表，名称，评论数，好评数，追评数，晒单数，差评数，图柱状图正常
# 分析：最爱的价格范围（饼图，总的分析），好评率和销量关系（双轴图：柱状图+折线图）
# 文件栏：只有保存功能编辑栏不做开发，窗口栏不做，显示可以设置（透明度啊啥的）语言新增
# 一个窗口设中英对照，帮助弹出，其他控件不做


class Html_file():
    def __init__(self):
        self.w_c()
        self.zonghe_bar(zonghe())
        self.xiaoliang_bar(xiaoliang(zonghe()))
        self.pinglun_bar(pinglun())
        self.price_best_pie(price_best())
        self.bar_line(fx_last())

    def w_c(self):
        myWordCloud = WordCloud()
        # 词云图
        myWordCloud.add("", JD_ciyun(), word_size_range=[20, 100])
        myWordCloud.set_global_opts(
            # 工具栏
            toolbox_opts=opts.ToolboxOpts(
                is_show=True,
                # 是否显示
                orient="vertical",
                # 布局朝向，可选：'horizontal', 'vertical'

                pos_left="90%",
                # 工具栏组件离容器左侧的距离
                # left 的值可以是像 20 这样的具体像素值，可以是像 '20%' 这样相对于容器高宽的百分比，
                # 也可以是 'left', 'center', 'right'，'top', 'middle', 'bottom'
                # 如果 left 的值为'left', 'center', 'right'，组件会根据相应的位置自动对齐
            )
        )
        myWordCloud.render('ciyun.html')


    def zonghe_bar(self,zonghe):
        b = Bar()
        # 柱状图-按好评数排序
        b.add_xaxis(zonghe[0])
        b.add_yaxis("评论数", zonghe[2])
        b.add_yaxis("销量", zonghe[1])
        b.set_global_opts(
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
            title_opts=opts.TitleOpts(title="综合排序", subtitle="只取综合前十列出"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
        )
        b.render("zonghebar.html")


    def xiaoliang_bar(self,xiaoliang):
        b = Bar()
        # 柱状图-按好评数排序
        x = []
        y = []
        for i in range(len(xiaoliang)):
            x.append(xiaoliang[i][0])
            y.append(xiaoliang[i][1])
        b.add_xaxis(x)
        b.add_yaxis("销量", y)
        b.set_global_opts(
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
            title_opts=opts.TitleOpts(title="综合排序", subtitle="只取综合前十列出"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
        )
        b.render("xiaoliangbar.html")


    def pinglun_bar(self,pinglun):
        b = Bar()
        # 柱状图-按默认排序
        b.add_xaxis(pinglun[0])
        b.add_yaxis("评论数", pinglun[1])
        b.add_yaxis("好评数", pinglun[2])
        b.set_global_opts(
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
            title_opts=opts.TitleOpts(title="综合排序", subtitle="注意：这里的好评数不计算默认好评"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
        )
        b.render("pinglunbar.html")


    def price_best_pie(self,price_best):
        pie = Pie()
        pie.add(
            "",
            [list(z) for z in price_best],
            radius=["40%", "55%"],
            label_opts=opts.LabelOpts(
                position="outside",
                formatter="{a|{a}}{abg|}\n{hr|}\n {b|{b}: }{c}  {per|{d}%}  ",
                background_color="#eee",
                border_color="#aaa",
                border_width=1,
                border_radius=4,
                rich={
                    "a": {"color": "#999", "lineHeight": 22, "align": "center"},
                    "abg": {
                        "backgroundColor": "#e3e3e3",
                        "width": "100%",
                        "align": "right",
                        "height": 22,
                        "borderRadius": [4, 4, 0, 0],
                    },
                    "hr": {
                        "borderColor": "#aaa",
                        "width": "100%",
                        "borderWidth": 0.5,
                        "height": 0,
                    },
                    "b": {"fontSize": 16, "lineHeight": 33},
                    "per": {
                        "color": "#eee",
                        "backgroundColor": "#334455",
                        "padding": [2, 4],
                        "borderRadius": 2,
                    },
                },
            ),
        )
        pie.set_global_opts(title_opts=opts.TitleOpts(title="最爱的价格分析"))
        pie.render("pie_fenxi.html")


    def bar_line(self,fx_last):

        bar = (
            Bar()
            .add_xaxis(fx_last[0])
            .add_yaxis(
                "销量",
                fx_last[1],
                color="#5793f3",
            )
            .extend_axis(
                yaxis=opts.AxisOpts(
                    name="好评率",
                    type_="value",
                    position="right",
                    axisline_opts=opts.AxisLineOpts(
                        linestyle_opts=opts.LineStyleOpts(color="#d14a61")
                    ),
                    axislabel_opts=opts.LabelOpts(formatter="{value}"),
                )
            )
            .set_global_opts(
                yaxis_opts=opts.AxisOpts(
                    name="销量",
                    position="right",
                    offset=80,
                    axisline_opts=opts.AxisLineOpts(
                        linestyle_opts=opts.LineStyleOpts(color="#5793f3")
                    ),
                ),
                title_opts=opts.TitleOpts(title="潜力性",subtitle="注意：这里的好评率=好评数/评论数 \n 即好评数不包括默认好评"),
                tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross"),
            )
        )

        line = (
            Line()
            .add_xaxis(fx_last[0])
            .add_yaxis(
                "好评率",
                fx_last[2],
                yaxis_index=1,
                color="#5793f3",

                label_opts=opts.LabelOpts(is_show=False),
            )
        )

        bar.overlap(line)
        grid = Grid()
        grid.add(bar, opts.GridOpts(pos_left="5%", pos_right="20%"), is_control_axis_index=True)
        grid.render("grid_guanxi.html")

